package com.htj.admin.controller;

import cn.hutool.core.util.IdUtil;
import com.htj.admin.domain.Food;
import com.htj.admin.domain.FoodType;
import com.htj.admin.domain.Order;
import com.htj.admin.domain.OrderDetail;
import com.htj.admin.service.FoodTypeServiceInf;
import com.htj.admin.service.OrderServiceInf;
import com.htj.common.Result;
import com.htj.vo.PageListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "order")
@CrossOrigin
public class OrderController {
    @Autowired
    OrderServiceInf orderServiceInf;
    /**
     * 新增订单
     * @param
     * @return
     */
    @PostMapping("/save")
    public Result<String> add(@RequestBody Order order){
        boolean bool = orderServiceInf.add(order);
        return bool?Result.succ():Result.fail();
    }

    /**
     * 分页数据
     * @param currentPage
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/pageList")
    public Result<PageListVo> pageList(@RequestParam String currentPage,
                                       @RequestParam String pageSize,
                                       @RequestParam String userName){
        HashMap<String, String> map = new HashMap<>();
        map.put("currentPage",currentPage);
        map.put("pageSize",pageSize);
        map.put("userName",userName);
        PageListVo<Order> pageList = orderServiceInf.pageList(map);
        return Result.succ(pageList);
    }

    /**
     * 删除订单
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public Result<String> delete(@PathVariable Integer id){
        boolean bool = orderServiceInf.delete(id);
        return bool?Result.succ():Result.fail();
    }

    @PostMapping("/changeStatus")
    public Result<String> changeStatus(@RequestBody Map<String,String> map){
        boolean bool = orderServiceInf.changeStatus(map);
        return bool?Result.succ():Result.fail();
    }

    @GetMapping("/detail")
    public Result<List<OrderDetail>> detail(@RequestParam String id){
        List<OrderDetail> list = orderServiceInf.detail(id);
        return Result.succ(list);
    }

    @GetMapping("/searchStatus")
    public Result<PageListVo> searchStatus(@RequestParam String currentPage,
                                       @RequestParam String pageSize,
                                       @RequestParam String status){
        HashMap<String, String> map = new HashMap<>();
        map.put("currentPage",currentPage);
        map.put("pageSize",pageSize);
        map.put("status",status);
        PageListVo<Order> pageList = orderServiceInf.searchStatus(map);
        return Result.succ(pageList);
    }
    @GetMapping("/buy/{bookId}")
    public Result<?> buy(@PathVariable Long bookId) {
        Food food = new Food();
        food.setName("喜羊羊");
        String orderNo = IdUtil.getSnowflake().nextIdStr();
        String payUrl = "http://localhost:9001/order/alipay/pay?subject=" + food.getName() + "&traceNo=" + orderNo + "&totalAmount=" + "999";
        // 新建订单，扣减库存
        return Result.succ(payUrl);
    }
}
